VBScript program to find objects in Active Directory using the LDAP provider, given only the cn (Common Name) attribute. Common Names must be unique in the container or Organizational Unit, but there can be many objects with the same Common Name in a domain. If the program finds just one object with the specified cn property, the Distinguished Name is displayed. If more than one object is found, the Distinguished Names of all the objects are displayed. The Distinguished Name can be used to bind to the object with the LDAP Provider.

The program has been revised to display the ObjectCategory (in parenthesis) as well as the Distinguished Name of all objects found with the specified Common Name. If ObjectCategory is "person", the object is a user or contact. ObjectCategory can also be "group", "computer", "container", and other possibilities.

The cn (Common Name) you are searching for is specified as a parameter. The value you specify can include the wildcard character "*". For example, at a command prompt you can run the program as follows to find all objects where the Common Name begins with the string "Test".

cscript //nologo CommonName.vbs "Test*"

The program has a variable called strFilter that specifies the objects retrieved from Active Directory. This variable is defined in the program by the statement:

strFilter = "(cn=" & strCN & ")"

If you want to restrict the objects found by the program to user objects, you can change this statement to:

strFilter = "(&(objectCategory=person)(objectClass=user)(cn=" & strCN & "))"

To restrict the objects found to computer objects, change the statement to:

strFilter = "(&(objectCategory=computer)(cn=" & strCN & "))"

To restrict the objects found to group objects, change the statement to:

strFilter = "(&(objectCategory=group)(cn=" strCN & "))"

CommonName.txt <<-- Click here to view or download the program